<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/11
 * Time: 9:58
 */

namespace App\Http\Controllers\Api\V1;


use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\RecommendPositionService;

class RecommendController extends Controller{

    /**
     * @api {post} /recommend/get_recommend_position 获取推荐位置
     * @apiName Recommend/getRecommendPosition
     * @apiGroup Recommend
     * @apiDescription 获取推荐位置
     * @apiSuccess {Object[]}  Object   推荐位置信息
     * @apiSuccess {int} Object.id 推荐位置ID
     * @apiSuccess {string} Object.name 推荐位置名称
     * @apiSuccess {string} Object.description 推荐位置描述
     * @apiSuccess {string} Object.identification 推荐位置标识
     * @apiSuccess {int} Object.is_recommend 是否推荐
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "id": 2,
        "name": "全球精选",
        "description": "首页全球精选",
        "identification": "index_jingxuan",
        "is_recommend": 1
    }
     */
    public function getRecommendPosition($identification,RecommendPositionService $recommendPositionService){
        $recommendPosition = $recommendPositionService->getRecommendPosition($identification);
        if($recommendPosition){
            return response()->json($recommendPosition);
        }else{
            return null;
        }

    }

    /**
     * @api {post} /recommend/get_recommend_goods/{identification} 获取推荐
     * @apiName Recommend/getRecommendGoods
     * @apiGroup Recommend
     * @apiDescription 获取推荐
     * @apiParam {string} identification  推荐位标识
     * @apiSuccess {Object[]}  Object   推荐信息
     * @apiSuccess {int} Object.id 推荐内容id
     * @apiSuccess {string} Object.recommend_position_id 推荐位id
     * @apiSuccess {string} Object.relation_id 关联的内容的id（可以为商品、活动等）
     * @apiSuccess {string} Object.title 标题
     * @apiSuccess {int} Object.subtitle 副标题
     * @apiSuccess {int} Object.description 描述
     * @apiSuccess {string} Object.link 链接
     * @apiSuccess {string} Object.sort_order 排序
     * @apiSuccess {string} Object.picture 推荐位图片
     * @apiSuccess {ind} Object.market_price 市场价
     * @apiSuccess {int} Object.shop_price 商城价
     * @apiSuccess {int} Object.is_on_sale 是否上架
     * @apiSuccess {string} Object.mobile_picture 推荐位图片地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
            "id": 86,
            "recommend_position_id": 2,
            "relation_id": 1336,
            "title": "雅诗兰黛特润修护肌透精华露 30ml",
            "subtitle": null,
            "description": "雅诗兰黛特润修护肌透精华露 30ml",
            "link": "1336",
            "sort_order": 0,
            "picture": "02cb81ab12354f2beab3a17fbf4750d4",
            "market_price": "660.00",
            "shop_price": "560.00",
            "is_on_sale": 1，
            "mobile_picture": "http://dolia.img-cn-hangzhou.aliyuncs.com/67155ab6af8718f7c399632549660bcd.jpg"
        }
    ]
     */
    public function getRecommendGoods($identification,RecommendPositionService $recommendPositionService){
        if(empty($identification)){
            throw new ApiInputErrorException();
        }
        $recommendGoods = $recommendPositionService->getRecommendGoods($identification);
        if($recommendGoods){
            return response()->json($recommendGoods);
        }else{
            return null;
        }

    }

    /**
     * @api {post} /recommend/get_all_recommend 获取所有推荐位及推荐
     * @apiName Recommend/getAllRecommend
     * @apiGroup Recommend
     * @apiDescription 获取推荐
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
    {
    "id": 1,
    "name": "爆品推荐",
    "description": "首页爆品",
    "identification": "index_baopin",
    "is_recommend": 1,
    "recommend": []
    },
    {
    "id": 2,
    "name": "全球精选",
    "description": "首页全球精选",
    "identification": "index_jingxuan",
    "is_recommend": 1,
    "recommend": [
    {
    "id": 86,
    "recommend_position_id": 2,
    "relation_id": 1336,
    "title": "雅诗兰黛特润修护肌透精华露 30ml",
    "subtitle": null,
    "description": "雅诗兰黛特润修护肌透精华露 30ml",
    "link": "1336",
    "sort_order": 0,
    "picture": "02cb81ab12354f2beab3a17fbf4750d4",
    "market_price": "660.00",
    "shop_price": "560.00",
    "is_on_sale": 1，
    "mobile_picture": "http://dolia.img-cn-hangzhou.aliyuncs.com/02cb81ab12354f2beab3a17fbf4750d4.jpg"
    }
    ]
    },
    {
    "id": 3,
    "name": "热卖爆品",
    "description": "家居生活热卖爆品",
    "identification": "cat_baopin",
    "is_recommend": 1,
    "recommend": []
    },
    {
    "id": 4,
    "name": "美妆个护",
    "description": "首页美妆个护",
    "identification": "index_recommend_cat_3",
    "is_recommend": 1,
    "recommend": [
    {
    "id": 95,
    "recommend_position_id": 4,
    "relation_id": 1328,
    "title": "  Floradix 有机补铁片 80片",
    "subtitle": null,
    "description": "铁元片剂84粒片剂，更易携带.有机补铁，吸收率高达98%，自有平衡机制，当身体铁足够时会自动停止吸收，适合长期使用。",
    "link": "1328",
    "sort_order": 0,
    "picture": "b3a19e9b1f827847039c2a5c2587fcc7",
    "market_price": "153.60",
    "shop_price": "128.00",
    "is_on_sale": 1，
    "mobile_picture": "http://dolia.img-cn-hangzhou.aliyuncs.com/b3a19e9b1f827847039c2a5c2587fcc7.jpg"
    }
    ]
    }
    ]
     */
    public function getAllRecommend(RecommendPositionService $recommendPositionService){

        $data = $recommendPositionService->getAllRecommendPosition();
        if($data){
            foreach ($data as $key=>$value) {
                $identification = $value->identification;
                if(strstr($identification,"index_recommend_cat")){
                    $number=5;
                }else{
                    $number=4;
                }
                $data[$key]['recommend'] = $recommendPositionService->getRecommendByNumber($number,$identification);
            }
            return response()->json($data);
        }else{
            return null;
        }

    }

} 